<?php
class cReport extends BaseController{
	
	public function index(){
		$choix = "Choisir";
		$this->loadView("vMenu");
		$this->loadView("vReport");
		
		echo JsUtils::postFormAndBindTo("#deconnexion", "click", "cAccueil/deconnexion", "menu","#menu");
		echo JsUtils::postFormAndBindTo("#deconnexion", "click", "cAccueil/deconnexion", "menu","#menu");
		
		echo JsUtils::postFormAndBindTo("#button2", "click", "cReport/insert", "'#contact-form'","#test");
		echo JsUtils::postFormAndBindTo("#button1", "click", "cReport/consult", "'#contact-form'","#test");
		echo JsUtils::postFormAndBindTo("#myReport", "click", "cReport/consultUtil", "'#contact-form'","#test");
		
		//Auchargement de la page on va charger toutes les applications dans une liste
		$lesAppli=DAO::getAll("Application");
		echo JsUtils::doSomethingOn("#application","append","'<option value=".$choix.">".$choix."</option>'");
		foreach($lesAppli as $app){
			
			echo JsUtils::doSomethingOn("#application","append","'<option value=".$app->getId().">".$app->getLibelle()."</option>'");
		}
				
		
		//Je crois que cela ne sert pas
	/*	if(isset($_SESSION['id'])){
		
			$allReport = DAO::getAll("Report", "idUtilisateur=".$_SESSION['id']);
			
			foreach($allReport as $report){
					
				echo JsUtils::doSomethingOn("#d","append","'<li class=\'lib\' value=".$report->getId()."><a><b>".$report->getLibelle()."</b></a></li>'");
			}
		}*/
		
		
		echo JsUtils::postAndBindTo("#application", "change", "cReport/useCase", "{id:$(this).val()}","#test");
		echo JsUtils::postAndBindTo("#application", "change", "cReport/useCase", "{id:$('#useCase').empty()}","#useCase");
			
		
	}
	public function useCase(){
		$choix = "Choisir";
		
		if($_POST['id']!= "Choisir"){
			//On va chercher les cas d'utilisation qui correspondent à l'id de l'appli
			$useCases=DAO::getAll("useCase","idApplication=".$_POST['id']);
			
			echo JsUtils::doSomethingOn("#useCase","append","'<option value=".$choix.">".$choix."</option>'");
			foreach ($useCases as $case){			
				echo JsUtils::doSomethingOn("#useCase","append","'<option value=".$case->getCode().">".$case->getLibelle()."</option>'");
				
			}
			
			echo JsUtils::postAndBindTo("#useCase", "change", "cReport/consult", "{useCase:$(this).val()}","#test");
			}
	}

	public function consult(){
		
		if (isset($_SESSION['id'])) {
			$idUtilisateur = $_SESSION['id'];
		}
		else{
			$idUtilisateur = " ";
		}
		
			if(isset( $_POST['useCase'])){$codeUseCase= $_POST['useCase'];}else{$codeUseCase="";}
			if(isset( $_POST['libelle'])){$libelle= $_POST['libelle'];}else{$libelle="";}
	
			if(isset($_POST['descriptif'])){$descriptif=$_POST['descriptif'];}else{$descriptif="";}
			if(isset($_POST['userAction'])){$userAction=$_POST['userAction'];}else{$userAction="";}
			if(isset($_POST['mErreur'])){$systemMsg=$_POST['mErreur'];}else{$systemMsg="";}
		
			
			$lesReports=DAO::getAll("Report","code='".$codeUseCase."'");

			Gui::debutTable();

			foreach ($lesReports as $report){
				
				$nouveauReport=new Report("","",$idUtilisateur,$codeUseCase,$libelle,$descriptif,"",$userAction,$systemMsg);
				$reportExistant = new Report("",$report->getIdStatut(),$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),
				$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage());
			
				$searchEngine = new SearchEngine();
					
			    if ($searchEngine->compare($reportExistant,$nouveauReport) == true){
					    Gui::getExists($report);
						unset($nouveauReport,$reportExistant);
						
				}
		
			}
		
			Gui::finTable();
			Gui::responseMessage();
			
			echo JsUtils::postAndBindTo(".ligne","click","cReport/detail","{idReport:$(this).attr('value')}","#report");
			echo JsUtils::postFormAndBindTo(".updateSearch", "change", "cReport/consult", "'#contact-form'","#test");
			echo JsUtils::postFormAndBindTo("#mErreur", "keyup", "cReport/consult", "'#contact-form'","#test");
			
	}
	
	
	public function detail(){
		if(isset($_POST['idReport'])){
			$idReport = $_POST['idReport'];
			//On affiche les message des utilisateurs à propos du report affiché
			Gui::message($idReport);
		}
		//echo JsUtils::postFormAndBindTo("#addMessageReport","click","cSearchReport/insertResponse", "formSubmitMessage","#response");
		echo JsUtils::postAndBindTo(".resolu","click","cReport/resolvedReport","{messageResolu:$(this).attr('name')}","#r");
		
		echo JsUtils::postAndBindTo("#addMessageReport","click","cReport/insertResponse","{message:$('#message').val(),idReport:$('#idReport').attr('value')}","#report");
	}
	
	

	public function insertResponse(){
		// Contient id du report et celui de l'utilisateur
		if(!empty($_SESSION['id'])){
			$id = $_SESSION['id'];
		}
		if(!empty($_POST['message']) && !empty($_POST['idReport'])){
			$message = $_POST['message'];
			$idReport = $_POST['idReport'];
				
			$date = date("Y-m-d");
		
			$allMessage = DAO::getAll("Message");
				
			foreach ($allMessage as $messages){
		
				if($messages->getIdReport()==$idReport){
					$aMessage=true;
						
				}
			}
		
	
		
			$messageInsert = new Message($idReport,$id,$message,$date);
			$ret = DAO::insert($messageInsert);
		
			$reports = DAO::getAll("Report");
		
		
			foreach ($reports as $report){

				if($report->getId() == $idReport && isset($aMessage) == true && $report->getIdStatut()=="5"){
					
					$report1 = new Report($report->getId(),"5",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
					DAO::update($report1);
				
				}
				elseif($report->getId() == $idReport && isset($aMessage) == true){
					
					$report1 = new Report($report->getId(),"3",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
					DAO::update($report1);
		
				}
				elseif ($report->getId() == $idReport && isset($aMessage) != true){
					
					$report1 = new Report($report->getId(),"1",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
					DAO::update($report1);
				}
			}
			echo "<div id='report'>";
			Gui::message($idReport);
			echo "</div>";
			echo JsUtils::postAndBindTo("#addMessageReport","click","cSearchReport/insertResponse","{message:$('#message').val(),idReport:$('#idReport').attr('value')}","#report");
		
		}
		else{
			echo JsUtils::execute('alert("Veuillez saisir un message.")');
		}
	}
	
	
	public function insert(){
		
			if (!isset($_SESSION['id'])) {
				echo "Il vous faut être connecté pour ajouter un nouveau report.";
			}
			else{
				if (empty($_POST['useCase']) || empty($_POST['libelle']) || empty($_POST['descriptif']) || empty($_POST['userAction']) || empty($_POST['mErreur'])) {
					echo JsUtils::execute('alert("Veuillez remplir tous les champs.")');
				}
				else{
			
					 $codeUseCase= htmlentities($_POST['useCase']);
					 $idAppli= htmlentities($_POST['application']);
			         $libelleErr=htmlentities($_POST['libelle']);
			         $description=htmlentities($_POST['descriptif']);
			         $creationDate = date("Y-m-d");
			         $userAction=htmlentities($_POST['userAction']);
			         $systemMsg=htmlentities($_POST['mErreur']);
			          
			         $nouveauReport=new Report("","1",$_SESSION['id'],$codeUseCase,$libelleErr,$description,$creationDate,$userAction,$systemMsg);
			        $insert = DAO::insert($nouveauReport);
	
			        if($insert == true){
						echo "Report posté.";
					}else{
						echo "Envoi échoué, vérifier les données saisie.";	
					}
				}
	        }     
	}
	
	public function resolvedReport(){
		$messageResolu = explode("+",$_POST['messageResolu']);
		
		$message = DAO::getAll("Message","id=".$messageResolu[1]);
		$reports = DAO::getAll("Report");
	
		foreach ($reports as $report){
			foreach ($message as $aMessage){
				if($report->getId() == $messageResolu[0]){
					
					$report1 = new Report($report->getId(),"5",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$aMessage->getContenu());
					
					DAO::update($report1);
				}
			}
		}
	
	}
	
	
	
}


?>